package com.wzy.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wzy.entity.User;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class LoginAction extends ActionSupport {

    private String username="";
    private String password="";

    private User user;

    private String nextDispose;

    private Map<String,String> myMap = new HashMap<String, String>();

    @Override
    public void validate() {
        if (user.getName().isEmpty()){
            addFieldError("name","用户名不能为空");
        }
        if (user.getPassword().isEmpty()){
            addFieldError("pwd","密码不能为空");
        }
    }

    public String login() throws Exception {
        nextDispose = "fail";
        if ("admin".equals(user.getName()) && "admin".equals(user.getPassword())){

            HttpServletRequest request = ServletActionContext.getRequest();
            request.getSession().setAttribute("CURRENT_USER",user);

            nextDispose = "success";
        }
        myMap.put("name","哈哈哈");
        return SUCCESS;
    }

    @Override
    public String execute() throws Exception {
        if ("admin".equals(user.getName()) && "admin".equals(user.getPassword())){

            HttpServletRequest request = ServletActionContext.getRequest();
            request.getSession().setAttribute("CURRENT_USER",user);

            return SUCCESS;
        }
        return ERROR;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getNextDispose() {
        return nextDispose;
    }

    public void setNextDispose(String nextDispose) {
        this.nextDispose = nextDispose;
    }

    public Map<String, String> getMyMap() {
        return myMap;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }
}
